package com.safewind.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.safewind.model.AddressGroup;
import com.safewind.model.User;
import com.safewind.service.IAddressGroupService;
import com.safewind.service.IAddressService;
import com.safewind.service.IHistoryAddressService;
import com.safewind.util.StringUtil;

@Controller
@RequestMapping("/addressGroup")
public class AddressGroupController {
	@Resource
	private IAddressGroupService addressGroupService;

	@Resource
	private IHistoryAddressService historyAddressService;

	@Resource
	private IAddressService addressService;

	@RequestMapping("/getAllGroup")
	public ModelAndView getAllGroup(HttpServletRequest request,
			HttpSession session) {
		ModelAndView mav = new ModelAndView();
		User user = (User) session.getAttribute("user");
		List<AddressGroup> list = addressGroupService.getAllGroup(user
				.getPkUserId());
		mav.addObject("addrList", list);
		return mav;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Object addAddressGroup(HttpServletRequest request,
			HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		String groupName = request.getParameter("groupName");
		AddressGroup ag = new AddressGroup();
		ag.setAddTime(new Date());
		ag.setGroupName(groupName);
		ag.setGroupOrder((byte) 0);
		ag.setPkId(StringUtil.getUUID());
		ag.setRemark(null);
		ag.setUpdateTime(new Date());
		ag.setUserId(user.getPkUserId());
		AddressGroup group = addressGroupService.selectAddressGroupByName(
				groupName, user.getPkUserId());
		if (group != null && StringUtil.isNotBlank(group.getPkId())) {
			map.put("success", false);
			map.put("msg", "该分组名已存在");
		} else {
			addressGroupService.addGroup(ag);
			historyAddressService.addGroup(user.getPkUserId(), groupName);
			map.put("success", true);
		}
		return map;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object deleteAddressGroup(HttpServletRequest request,
			HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		String groupIds = request.getParameter("groupIds");
		String addressIds = request.getParameter("addressIds");
		User user = (User) session.getAttribute("user");
		if (StringUtil.isContains(groupIds, "all")) {
			addressGroupService.deleteAllByUserId(user.getPkUserId());
			historyAddressService.deleteAll(user.getPkUserId());
		} else {
			addressGroupService.deleteAddressGroupByIds(groupIds);
			addressService.deleteAddressByIds(addressIds);
			historyAddressService.deleteAddress(user.getPkUserId(),
					groupIds.split(",").length, addressIds.split(",").length);
		}
		map.put("success", true);
		return map;
	}
}
